<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 活动控制器
 */
class ActivityController extends Controller{
	/**
	 * 活动列表
	 */

		public function  index(){
			$re=M('activity')->Field("id,theme,opentime,endtime,activityimg")->select();

			foreach($re as $k=>$v){
				$activityid[]=$v['id'];
			}
			$where['activityid']=array("in",array_keys(array_flip($activityid)));///获取所有活动id
				$count=M('item')->where($where)->count();
				$pagearr= getPageArr($page,C('PAGESIZE',12),$count);
				$pagetotal[]=$pagearr['pagetotal'];
				$res=M('item')->where($where)->limit($pagearr['start'],$pagearr['pagesize'])->Field("itemid,itemname,bn,price,mkprice,itemimg,activityid")->select();///获取该活动下所有产品
					foreach ($res as $key => $value) {
						foreach($re as $k=>$v)
					{
						if($value['activityid']==$v['id']){
						$res[$key]['id']=$v['id'];
						$res[$key]['theme']=$v['theme'];
						$res[$key]['opentime']=$v['opentime'];
		                $res[$key]['endtime']=$v['endtime'];
		                $res[$key]['activityimg']=$v['activityimg'];

					}
					}
		}

		if(empty($res)){
			       $result['status']=0;
					$result['message']='暂无信息';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);

		}else{
                
			     $result['totalpage']=max($pagetotal);  
			     $result['item']=$res;
					
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
			     

		}

		}

/**
 * 活动列表
 */

		public function lists(){
			$re=M('activity')->Field("id,theme,opentime,endtime,activityimg,state")->select();
			if(!empty($re)){
				$res['status']=1;
				$res['msg']='成功';
				$res['activity']=$re;
				echo json_encode($res,JSON_UNESCAPED_UNICODE);
			}else{
				$result['status']=0;
					$result['msg']='暂无信息';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);

			}

		}


		/**
		 * 参与活动商品
		 */

      public function activityitem(){
	$where['activityid']=I('request.id',13,'strip_tags');
	            $count=M('item')->where($where)->count();
				$pagearr= getPageArr($page,C('PAGESIZE',12),$count);
				$pagetotal[]=$pagearr['pagetotal'];
				$res=M('item')->where($where)->limit($pagearr['start'],$pagearr['pagesize'])->Field("itemid,itemname,bn,price,mkprice,itemimg,activityid")->select();///获取该活动下所有产品
				if(!empty($res)){
					$result['status']=1;
				$result['msg']='成功';
					 $result['totalpage']=max($pagetotal);  
			         $result['item']=$res;
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
				}else{
					$result['status']=0;
					$result['msg']='暂无信息';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);


				}

}

}